package com.javasm.crm.service.sell.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.crm.domin.entity.sell.RepositorySellOutOrder;
import com.javasm.crm.mapper.sell.RepositorySellOutOrderMapper;
import com.javasm.crm.service.sell.IRepositorySellOutOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lqz
 * @since 2021-07-14
 */
@Service
@Transactional
public class RepositorySellOutOrderServiceImpl extends ServiceImpl<RepositorySellOutOrderMapper, RepositorySellOutOrder> implements IRepositorySellOutOrderService {

    @Autowired
    RepositorySellOutOrderMapper repositorySellOutOrderMapper;

    @Override
    public boolean addAll(List<RepositorySellOutOrder> repositorySellOutOrderList) {
        int j = 0;
        for (RepositorySellOutOrder repositorySellOutOrder : repositorySellOutOrderList) {
            //查找仓库数据库表单出库码
            List<String> sellOutCodeList = repositorySellOutOrderMapper.selectOutCodeList();
            if (sellOutCodeList != null && !sellOutCodeList.isEmpty()){
                ArrayList<Integer> integers = new ArrayList<>();
                for (String sellOutCode : sellOutCodeList) {
                    String substring = sellOutCode.substring(2);
                    Integer integerOutCode = Integer.valueOf(substring);
                    integers.add(integerOutCode);
                }
                //找到integers中的最大值
                Integer maxNum = 0;
                for (Integer integer : integers) {
                    if (maxNum < integer){
                        maxNum = integer;
                    }
                }
                //找到的最大值+1
                Integer num = Integer.valueOf(++maxNum);
                //转成字符串
                String numStr = num.toString();
                //声明一个字符串，用来合成数字部分
                String theNum = "";
                //数字不满4位数，在之前添加几个0
                if (numStr.length() < 4){
                    int size = 4 - numStr.length();
                    for (int i = 0; i < size; i++) {
                        theNum += "0";
                    }
                }
                theNum += numStr;
                String sellOutCode = "XS" + theNum;
                repositorySellOutOrder.setSellOutCode(sellOutCode);
            }else {
                repositorySellOutOrder.setSellOutCode("XS0001");
            }
            j = repositorySellOutOrderMapper.insert(repositorySellOutOrder);
            if (j <= 0){
                return false;
            }
        }
        return j > 0;
    }

    @Override
    public RepositorySellOutOrder searchOne(QueryWrapper<RepositorySellOutOrder> wrapper) {
        return repositorySellOutOrderMapper.selectOne(wrapper);
    }
}
